<!--
  Author: 善良的YWJ
  Created by shanliangdeYWJ on 2020/09/26
  File: 01.6父ref传入子节点.html
  Description: "
  
   请输入文件描述内容
  
               " 
  Github: https://github.com/shanliangdeYWJ
-->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
  <meta name="keywords" content="">
  <meta name="description" content="">
  <title>01.6父ref传入子节点</title>
  <style></style>
</head>

<body>

  <div id="root"></div>

  <!-- 加载 React。-->
  <!-- 注意: 部署时，将 "development.js" 替换为 "production.min.js"。-->
  <!--<script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
  <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
  <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>-->
  <script src="../js/react@16.development.js" crossorigin></script>
  <script src="../js/react-dom@16.development.js" crossorigin></script>
  <script src="../js/babel@6.min.js"></script>
  <!-- <script src="https://unpkg.com/react@16/umd/react.production.min.js" crossorigin></script>
  <script src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js" crossorigin></script> -->

  <!-- 加载我们的 React 组件。-->
  <script type="text/babel">
    const { Component } = React

    function CustomTextInput(props) {
      console.log(props.inputRef);
      return (
        <div>
          <input type="text" ref={props.inputRef} />
        </div>
      )
    }

    class Parent extends Component {
      constructor(props) {
        super(props);
        this.state = {};
        this.inputElement = React.createRef()
      }
      componentDidMount() { this.focus() }
      focus() {
        // 使用原始的 DOM API 显式地聚焦在 text input 上
        // 注意：我们通过访问“current” 来获得 DOM 结点
        this.inputElement.current.focus();
      }

      render() {
        return (
          <CustomTextInput inputRef={this.inputElement} />
        );
      }
    }

    ReactDOM.render(
      <Parent />,
      document.getElementById('root')
    );

  </script>
</body>

</html>